﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class LanguageManager
    {
        public LanguageInfo GetLanguageById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryLanguage = from d in db.Languages
                                    where d.Id.Equals(id)
                                    select new LanguageInfo { Id = d.Id,  Name = d.Name,Description=d.Description};

                return queryLanguage.First();
            }
        
        }


        public IList<LanguageInfo> ListLanguage()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryLanguage = from d in db.Languages
                                   select new LanguageInfo { Id = d.Id,Name = d.Name,Description=d.Description };

                return queryLanguage.ToList();
            }
        }
    

        public void CreateLanguage(Language Language)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Languages.InsertOnSubmit(Language);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Language"></param>
        public void UpdateLanguage(Language Language)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Language = db.Languages.Single(d => d.Id.Equals(Language.Id));

                _Language.Name = Language.Name;
                _Language.Description = Language.Description;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteLanguage(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Language = db.Languages.Single(d => d.Id.Equals(id));

                db.Languages.DeleteOnSubmit(_Language);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Languages
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Languages
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
